package com.tianxiabuyi.utils;

import java.util.List;

/**
 * 图片处理工具类
 * Created by yang on 2015/5/15.
 */
public final class ImageUtil {
    private ImageUtil() {
    }

    /**
     * 将网络图片抓取到image.eeesys.com
     * @param urls 图片地址(多个图片用|隔开)
     */
    public static String path(String urls, String size) {
        urls = URLEncodeUtil.encodeURL(urls);
        String url = "http://image.eeesys.com/remote_image.php?urls=" + urls;
        String html = HttpUtil.post(url);

        if (html.indexOf("Warning: fopen") > -1)  { // 读取网络图片失败
            return "-1";
        }
        if (((int) html.charAt(0)) > 32768) {
            html = html.substring(1, html.length());
        }
        String result = "";
        String[] fileNames = html.split("\\|");
        for (int i = 0; i < fileNames.length; i++) {
            url = ("http://image.eeesys.com/copy_image.php?file=" + fileNames[i])
                    .trim();
            result = result + "http://image.eeesys.com/" + size + "/"
                    + HttpUtil.post(url).trim() + "|";
        }

        if (result.length() > 0) {
            result = result.substring(0, result.length() - 1);
        }

        return result;
    }

    /**
     * 小尺寸地址
     */
    public static String small(String urls) {
        return path(urls, "small");
    }

    /**
     * 中尺寸地址
     */
    public static String medium(String urls) {
        return path(urls, "medium");
    }

    /**
     * 大尺寸地址
     */
    public static String big(String urls) {
        return path(urls, "big");
    }

    /**
     * 原始尺寸地址
     */
    public static String orig(String urls) {
        return path(urls, "orig");
    }

    /**
     * 根据网络图片地址获取尺寸
     */
    public static String size(String urls) {
        urls = URLEncodeUtil.encodeURL(urls);
        String url = "http://image.eeesys.com/size.php?urls=" + urls;
        return HttpUtil.get(url);
    }

    /**
     * 根据网络图片地址获取尺寸
     */
    public static String size(List<String> urls) {
        String url = "";
        for (int i = 0; i < urls.size(); i++) {
            url = url + "," + urls.get(i);
        }
        if (url.startsWith(",")) {
            url = url.substring(1, url.length());
        }
        return size(url);
    }
}